<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="5" :sm="24">
              <a-form-item label="项目名称">
                <a-input v-model="queryParam.projectName" placeholder="请输入" allow-clear />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <detail-form
        ref="detailForm"
      />
      <!-- 数据展示 -->
      <a-table :loading="loading" :size="tableSize" rowKey="menuId" :columns="columns" :data-source="list"
               :pagination="false" :bordered="tableBordered">
        <span slot="icon" slot-scope="text">
          <a-icon :component="allIcon[text + 'Icon']" v-if="allIcon[text + 'Icon']" />
          <a-icon :type="text" v-if="!allIcon[text + 'Icon']" />
        </span>
        <!-- <span slot="status" slot-scope="text, record">
            <dict-tag :options="dict.type['sys_normal_disable']" :value="record.status" />
          </span> -->
<!--        <span slot="pointType" slot-scope="text, record">-->
<!--          {{ dict.type['point_type'].find(item=>item.value == record.pointType)?.label ?? '' }}-->
<!--        </span>-->
        <span slot="applyType" slot-scope="text, record">
          {{ dict.type['apply_type'].find(item=>item.value == record.applyType)?.label ?? '' }}
        </span>
<!--        <span slot="createTime" slot-scope="text, record">-->
<!--          {{ parseTime(record.createTime) }}-->
<!--        </span>-->
        <span slot="operation" slot-scope="text, record">
           <a @click="$refs.detailForm.handleAdd(record)">
            <a-icon type="eye" />详情
          </a>
          <a-divider type="vertical" />
          <a @click="handleRedirectVehicle('agree', record)" v-hasPermi="['system:menu:edit']">
            <a-icon type="edit" />更换物流公司
          </a>
<!--          <a-divider type="vertical" v-hasPermi="['system:menu:add']" />-->
<!--          <a @click="handleShowModalFast(record)" v-hasPermi="['system:menu:edit']">-->
<!--            <a-icon type="edit" />快速审批-->
<!--          </a>-->
        </span>
      </a-table>
      <!-- 分页 -->
      <a-pagination class="ant-table-pagination" show-size-changer show-quick-jumper :current="queryParam.pageNum"
                    :total="total" :page-size="queryParam.pageSize" :showTotal="total => `共 ${total} 条`"
                    @showSizeChange="onShowSizeChange" @change="changeSize" />
    </a-card>
    <a-modal v-model:open="uploadOpen" title="重新分派" @ok="handleOk">
      <a-form-model ref="form" :model="form">
        <a-form-model-item label="派车超时的物流公司" prop="dept">
          <a-input v-model="form.dept" disabled="" placeholder="请输入" />
        </a-form-model-item>
        <a-form-model-item label="更换物流公司" prop="deptId" :rules="{ required: true, message: '请选择物流公司', trigger: 'blur' }">
          <a-select placeholder="请选择" v-model="form.deptId" @focus="getDeptOptions" >
            <a-select-option v-for="(d, index) in deptOptions" :key="index" :value="d.deptId">{{ d.deptName
              }}
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="指派类型" prop="assignType" :rules="{ required: true, message: '请选择指派类型', trigger: 'blur' }">
          <a-select placeholder="请选择指派类型" v-model="form.assignType">
            <a-select-option v-for="(d, index) in dict.type['assign_type']" :key="index" :value="d.value">{{ d.label
              }}</a-select-option>
          </a-select>
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <a-modal v-model:open="openFast" title="快速审批" @ok="handleOkFast">
      是否快速审批？
    </a-modal>
  </page-header-wrapper>
</template>

<script>

import allIcon from '@/core/icons'
import { tableMixin } from '@/store/table-mixin'
import { redirectVehicle, getListByStatus } from '@/api/vehicle/orderinfo'
import { getChildrenDept } from '@/api/system/dept'
import baseUpload from '@/components/imgUpload'
import DetailForm from './modules/DetailForm.vue'

export default {
  name: 'Menu',
  components: {
    DetailForm,
    baseUpload
  },
  mixins: [tableMixin],
  dicts: ['sys_normal_disable', 'sys_show_hide','assign_type','point_type','apply_type'],
  data() {
    return {
      allIcon,
      iconVisible: false,
      list: [],
      loading: false,
      total: 0,
      openFast: false,
      queryParam: {
        projectName: undefined,
        visible: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      uploadOpen: false,
      columns: [
        {
          title: '项目名称',
          dataIndex: 'projectName',
          ellipsis: true
        },
        {
          title: '项目经理',
          dataIndex: 'projectManager',
          align: 'center',
          ellipsis: true
        },
        {
          title: '发货时间',
          dataIndex: 'deliverDate',
          align: 'center',
          ellipsis: true
        },
        {
          title: '发货原因说明',
          dataIndex: 'deliverDesc',
          align: 'center',
          ellipsis: true
        },
        {
          title: '始发地',
          dataIndex: 'beginAddr',
          align: 'center',
          ellipsis: true
        },{
          title: '目的地',
          dataIndex: 'endAddr',
          align: 'center',
          ellipsis: true
        },
        {
          title: '当前节点',
          dataIndex: 'pointType',
          ellipsis: true,
          align: 'center',
          // scopedSlots: { customRender: 'pointType' },
        },
        {
          title: '当前审批状态',
          dataIndex: 'applyType',
          ellipsis: true,
          align: 'center',
          scopedSlots: { customRender: 'applyType' },
        },
        {
          title: '审批时间',
          dataIndex: 'finishTime',
          ellipsis: true,
          align: 'center'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          ellipsis: true,
          // scopedSlots: { customRender: 'createTime' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: '20%',
          scopedSlots: { customRender: 'operation' },
          align: 'center'
        }
      ],
      projectManagerOptions: [{ value: '左洋', key: '0' },
        { value: '陈通', key: '1' },
        { value: '施梦洋', key: '2' }],
      deliveryMethodOptions: [],
      loadingPersonnelOptions: [ { value: '装车人员一', key: '0'},
        { value: '装车人员一', key: '1'}],
      deptOptions: [],
      driverOptions: [{ value: '王二' },
        { value: '张三' }],
      vehicleOptions: [
        {
          value: '大货车-鲁A12345',
          img: 'http://111.35.148.38:10002/profile/upload/2024/07/20/b880b73185fa48bfbf3534e755ed542c_20240720112839A001.jpeg'
        },
        {
          value: '小货车-鲁B56822',
          img: 'http://111.35.148.38:10002/profile/upload/2024/07/20/IMG0015609e155440930189489_20240720113119A001.jpg'
        }],
      eqTypeOptions: [],
      needVehicleOptions: [],
      form: {
        id: undefined,
        deptId: undefined,
        assignType: undefined,
      },
      detailsType: undefined,
      detailsId: undefined,
      remark: undefined
    }
  },
  created() {
    this.getList()
  },
  computed: {
  },
  watch: {
  },
  methods: {
    getDeptOptions(){
      this.loading = true
      getChildrenDept(110).then(response => {
        this.deptOptions = response.data
        this.loading = false
      })
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true
      getListByStatus(this.queryParam,'5').then(response => {
          console.log(this.queryParam)
          this.list = response.rows
        this.list.map(item => ({
          ...item,
          createTime: item.createTime ? this.parseTime(item.createTime).toString() : '',
          finishTime: item.finishTime ? this.parseTime(item.finishTime).toString() : '',
          pointType: item.pointType =this.dict.type['point_type'].find(i=>i.value == item.pointType)?.label ?? '',
        }))
          this.total = response.total
          this.loading = false
        }
      )
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParam = {
        memuName: undefined,
        status: undefined
      }
      this.handleQuery()
    },
    /** 查询菜单下拉树结构 */
    handleRedirectVehicle(type, record) {
      this.reset()
      this.form.dept = record.dept
      // this.form.projectManager = record.projectManager
      // this.form.deliveryMethod = record.deliveryMethod
      this.uploadOpen = !this.uploadOpen
      this.detailsType = type
      // this.detailsId = record.id
      // this.remark = record.remark
      this.form.id = record.id

    },
    handleShowModalFast(record) {
      this.detailsId = record.id
      this.remark = record.remark
      this.openFast = !this.openFast
      this.detailsType = 'agree'
    },
    onShowSizeChange(current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    changeSize(current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        comments: undefined,
        imgLinks: 'http://111.35.148.38:10002/profile/upload/2024/07/20/b880b73185fa48bfbf3534e755ed542c_20240720112839A001.jpeg',
        infoId: undefined,
        company: '德邦物流',
        driver: '王二',
        phone: '18265624444',
        vehicle: '大货车-鲁A12345'
        // cost: undefined,
      }
      this.remark = undefined
      this.detailsId = undefined
      this.detailsType = undefined
    },
    handleOkFast() {
      approvalFlow({ result: this.detailsType, processInstanceId: this.remark }).then(() => {
        this.$message.success(
          '审批成功',
          3
        )
        this.openFast = false
        this.getList()
      })
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 如果所有规则都满足，执行提交逻辑
          this.uploadOpen = true
          redirectVehicle(this.form).then(() => {
            this.$message.success(
              '分派成功',
              3
            )
            this.uploadOpen = false
            this.getList()
          })
        } else {
          // 如果有规则未满足，阻止表单提交
          return false;
        }
      });
    },
    handleImg(val) {
      const data = this.vehicleOptions.find((item) => item.value === val)
      this.form.imgLinks = data.img
    }
  }
}
</script>
<style lang="less" scoped>
.ant-form-item {
  margin-bottom: 5px;
}

.ant-modal-body {
  padding: 0 24px
}

.ant-modal {
  top: 30px;
}
</style>